﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CCWin;

namespace HotelManager.Views
{
    public partial class Form_RoomManage : Skin_DevExpress
    {
        public Form_RoomManage()
        {
            InitializeComponent();
        }

        private void Form_RoomManage_Load(object sender, EventArgs e)
        {
            RefreshGrid1();
        }

        private void RefreshGrid1()
        {
            skinDataGridView1.DataSource = DB.GetDs("select * from 房间类型表").Tables[0].DefaultView;
            skinComboBox1.DataSource = DB.GetDs("select 类型ID,客房类型 from 房间类型表").Tables[0].DefaultView;
            skinComboBox1.DisplayMember = "客房类型";
            skinComboBox1.ValueMember = "类型ID";
        }

        private void UpdateRooms()
        {
            skinTextBox7.Text = skinDataGridView2.CurrentRow.Cells[1].Value.ToString();
            skinTextBox8.Text = skinDataGridView2.CurrentRow.Cells[6].Value.ToString();
            skinLabel11.Text = skinDataGridView2.CurrentRow.Cells[0].Value.ToString();
            skinComboBox1.Text = DB.GetData("select 客房类型 from 房间类型表,房间状态表 where 房间状态表.类型ID=房间类型表.类型ID and 房间状态表.房间号=" + skinTextBox7.Text).ToString();
        }

        private void UpdateTypes()
        {
            skinDataGridView2.DataSource = DB.GetDs("select * from 房间状态表 where 类型ID="
                + skinDataGridView1.CurrentRow.Cells[0].Value.ToString()).Tables[0].DefaultView;

            skinLabel2.Text = skinDataGridView1.CurrentRow.Cells[0].Value.ToString();
            skinTextBox1.Text = skinDataGridView1.CurrentRow.Cells[1].Value.ToString();
            skinTextBox2.Text = skinDataGridView1.CurrentRow.Cells[2].Value.ToString();
            skinTextBox3.Text = skinDataGridView1.CurrentRow.Cells[3].Value.ToString();
            skinTextBox4.Text = skinDataGridView1.CurrentRow.Cells[4].Value.ToString();
            skinTextBox5.Text = skinDataGridView1.CurrentRow.Cells[5].Value.ToString();
            skinTextBox6.Text = skinDataGridView1.CurrentRow.Cells[6].Value.ToString();
        }


        private void skinDataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            UpdateTypes();
        }

        private void skinDataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            UpdateRooms();
        }
        private void skinButton1_Click(object sender, EventArgs e)
        {
            if (skinTextBox1.Text == string.Empty || skinTextBox2.Text == string.Empty ||
                skinTextBox3.Text == string.Empty || skinTextBox4.Text == string.Empty ||
                skinTextBox5.Text == string.Empty || skinTextBox6.Text == string.Empty)
            {
                MessageBox.Show("请先输入数据完全！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (DB.GetDs("select * from 房间类型表 where 客房类型='"
                + skinTextBox1.Text + "'").Tables[0].DefaultView.Count > 0)
            {
                MessageBox.Show("当前房型已经存在，请重新输入！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                string sql = string.Format(@"insert into 房间类型表(客房类型,面积,床位,单价,押金,备注) 
                                             values('{0}', {1}, {2}, {3}, {4}, '{5}')",
                                             skinTextBox1.Text,
                                             skinTextBox2.Text,
                                             skinTextBox3.Text,
                                             skinTextBox4.Text,
                                             skinTextBox5.Text,
                                             skinTextBox6.Text);
                DB.Execute(sql);
                RefreshGrid1();
                MessageBox.Show("成功添加该房型！", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void skinButton6_Click(object sender, EventArgs e)
        {
            if (skinTextBox7.Text == string.Empty || skinTextBox8.Text == string.Empty)
            {
                MessageBox.Show("请先输入数据完全！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (DB.GetDs("select * from 房间状态表 where 客房类型='"
                + skinTextBox7.Text + "'").Tables[0].DefaultView.Count > 0)
            {
                MessageBox.Show("当前房间号已经存在，请重新输入！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                string sql = string.Format(@"insert into 房间状态表(房间号,类型ID,是否预订,是否空,是否停用,备注) 
                                             values({0}, {1}, {2}, {3}, {4}, '{5}')",
                                             skinTextBox7.Text,
                                             skinComboBox1.SelectedValue.ToString(),
                                             "否",
                                             "是",
                                             "否",
                                             skinTextBox8.Text);
                DB.Execute(sql);
                UpdateTypes();
                MessageBox.Show("成功添加该房间！", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void skinButton2_Click(object sender, EventArgs e)
        {
            if (skinTextBox1.Text == string.Empty || skinTextBox2.Text == string.Empty ||
                skinTextBox3.Text == string.Empty || skinTextBox4.Text == string.Empty ||
                skinTextBox5.Text == string.Empty || skinTextBox6.Text == string.Empty)
            {
                MessageBox.Show("请先输入数据完全！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (DB.GetDs("select * from 房间类型表 where 客房类型='"
                + skinTextBox1.Text + "'").Tables[0].DefaultView.Count > 0)
            {
                MessageBox.Show("当前房型已经存在，请重新输入！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                string sql = string.Format(@"update into 房间类型表
                                            set 客房类型='{0}',面积={1},床位={2},单价={3},押金={4},备注='{5}'
                                            where 类型ID={6}",
                                            skinTextBox1.Text,
                                            skinTextBox2.Text,
                                            skinTextBox3.Text,
                                            skinTextBox4.Text,
                                            skinTextBox5.Text,
                                            skinTextBox6.Text,
                                            skinLabel2.Text);
                DB.Execute(sql);
                RefreshGrid1();
                MessageBox.Show("成功修改该房型！", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void skinButton5_Click(object sender, EventArgs e)
        {
            if (skinTextBox7.Text == string.Empty || skinTextBox8.Text == string.Empty)
            {
                MessageBox.Show("请先输入数据完全！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (DB.GetDs("select * from 房间状态表 where 客房类型='"
                + skinTextBox7.Text + "'").Tables[0].DefaultView.Count > 0)
            {
                MessageBox.Show("当前房间号已经存在，请重新输入！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                string sql = string.Format(@"update 房间状态表
                                            set 房间号={0},类型ID={1},是否预订={2},是否空={3},是否停用={4},备注='{5}'
                                            where 房间ID={6}",
                                            skinTextBox7.Text,
                                            skinComboBox1.SelectedValue.ToString(),
                                            "否",
                                            "是",
                                            "否",
                                            skinTextBox8.Text,
                                            skinLabel11.Text);
                DB.Execute(sql);
                UpdateTypes();
                MessageBox.Show("成功修改该房间！", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void skinButton3_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确实要删除这个房间类型吗？该类型下的所有房间都将被删除！", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                MessageBox.Show("错误！当前做法过于激进，已被系统阻止！", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void skinButton4_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确实要删除这个房间吗？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                DB.Execute("delete from 房间状态表 where 房间ID="+skinLabel11.Text);
                MessageBox.Show("删除该房间成功！", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
